﻿#include "lvgl_backlight.h"
#include "lvgl_backlight_anim.h"


#if 1
#define lvgl_backlight_debug(format, ...) lvgl_project_debug("[lvgl backlight]-",format,##__VA_ARGS__);
#else
#define lvgl_backlight_debug(format, ...) ;
#endif



__lvgl_backlight_data lvgl_backlight_data;
// = {
//     .close_data.task = NULL,
//     .close_data.flag = false,
//     .close_data.count = 0,
//     .close_data.num = 0,


//     .cont_main = NULL,
//     .task = NULL,
//     .anim_in = NULL,
//     .anim_out = NULL,
//     .anim_path_in = NULL,
//     .anim_path_out = NULL,
// };


static void lvgl_backlight_set_backlight(int dat);

void lvgl_backlight_task_cb(lv_task_t* t)
{


}
void lvgl_backlight_close_task_cb(lv_task_t* t)
{
    static uint8_t cnt = 0;
    if (lvgl_backlight_data.close_data.flag == true)
    {

        if (++cnt >= lvgl_backlight_data.close_data.count)
        {
            cnt = 0;
            lvgl_backlight_data.close_data.flag = false;
            lvgl_task_delete(&lvgl_backlight_data.close_data.task);

            if(lvgl_backlight_data.close_data.num == 0)
            {
                lvgl_backlight_anim_out_start(0, 350);

            }else
            if(lvgl_backlight_data.close_data.num == 1)
            {
                lvgl_backlight_anim_out_start(0, 350);
            }else
            if(lvgl_backlight_data.close_data.num == 2)
            {
                lvgl_backlight_anim_out_start(0, 250);
            }

        }

    }

}



void lvgl_backlight_event_cb(struct _lv_obj_t* obj, lv_event_t event)
{
        if (obj == lvgl_backlight_data.cont_main)
        {

            //lvgl_backlight_debug("cb:%d", event);
            switch (event)
            {

            case LV_EVENT_KEY:

                switch (*(const uint32_t*)lv_event_get_data())
                {

                case LV_KEY_UP:
                    lvgl_backlight_debug("LV_KEY_UP");

                    break;
                case LV_KEY_DOWN:
                    lvgl_backlight_debug("LV_KEY_DOWN");

                    break;
                case LV_KEY_LEFT:
                    lvgl_backlight_debug("LV_KEY_LEFT");

                    system_data.backlight-=5;
                    if(system_data.backlight<=5)
                    {
                        system_data.backlight = 5;

                    }
                    lvgl_backlight_set_backlight(system_data.backlight);



                    break;
                case LV_KEY_RIGHT:
                    lvgl_backlight_debug("LV_KEY_RIGHT");

                    system_data.backlight+=5;
                    if(system_data.backlight>=100)
                    {
                        system_data.backlight = 100;

                    }

                    lvgl_backlight_set_backlight(system_data.backlight);

                    break;
                case LV_KEY_ESC:
                    lvgl_backlight_debug("LV_KEY_ESC");



                    lvgl_backlight_close_task(0);

                    break;
                case LV_KEY_DEL:
                    lvgl_backlight_debug("LV_KEY_DEL");

                    break;
                case LV_KEY_BACKSPACE:
                    lvgl_backlight_debug("LV_KEY_BACKSPACE");

                    break;
                case LV_KEY_ENTER:
                    lvgl_backlight_debug("LV_KEY_ENTER");

                    lvgl_backlight_close_task(0);

                    break;
                case LV_KEY_NEXT:
                    lvgl_backlight_debug("LV_KEY_NEXT");

                    break;
                case LV_KEY_PREV:
                    lvgl_backlight_debug("LV_KEY_PREV");

                    break;
                case LV_KEY_HOME:
                    lvgl_backlight_debug("LV_KEY_HOME");


                    break;
                case LV_KEY_END:
                    lvgl_backlight_debug("LV_KEY_END");
                    break;
                }

                break;



            case LV_EVENT_DELETE:

                lvgl_backlight_data.cont_main = NULL;
                lvgl_backlight_debug("delete win");
                break;

            case LV_EVENT_PRESSING://对象被持续按下

                break;
            case LV_EVENT_PRESSED://对象已被按下

                break;
            case LV_EVENT_DRAG_END://拖动结束后

                if (lv_obj_get_x(lvgl_backlight_data.cont_main) != 0)
                {
                    lv_obj_set_x(lvgl_backlight_data.cont_main, 0);
                }

                if (lv_obj_get_y(lvgl_backlight_data.cont_main) != 0)
                {
                    lv_obj_set_y(lvgl_backlight_data.cont_main, 0);
                }
                break;
            case LV_EVENT_RELEASED://按钮释放

            case LV_EVENT_LONG_PRESSED://按钮长按

                break;

            default:
                break;
            }
        }
}


void lvgl_backlight_create(lv_obj_t* parent)
{


    if (lvgl_backlight_data.cont_main == NULL)
    {

        //----创建容器----//
        lvgl_backlight_data.cont_main = lv_cont_create(parent, NULL);

        lv_obj_set_size(lvgl_backlight_data.cont_main, 280,130);

        lv_obj_set_click(lvgl_backlight_data.cont_main, true); //启用 / 禁用可点击
        lv_obj_set_drag(lvgl_backlight_data.cont_main, true);//启用/禁用对象可拖动
        lv_obj_set_drag_throw(lvgl_backlight_data.cont_main, true);//启用/禁用拖动后是否有惯性移动
        lv_obj_set_drag_parent(lvgl_backlight_data.cont_main, false); //启用 / 禁用父对象可拖动
        lv_obj_set_drag_dir(lvgl_backlight_data.cont_main, LV_DRAG_DIR_HOR);//设置拖动方向
        lv_obj_add_style(lvgl_backlight_data.cont_main, LV_OBJ_PART_MAIN, &lvgl_style.cont_no_border);
        lv_obj_set_event_cb(lvgl_backlight_data.cont_main, lvgl_backlight_event_cb);

        lv_obj_align(lvgl_backlight_data.cont_main, NULL, LV_ALIGN_CENTER, 0, 0);
        lvgl_backlight_data.main_cont_x = lv_obj_get_x(lvgl_backlight_data.cont_main);
        lvgl_backlight_data.main_cont_y = lv_obj_get_y(lvgl_backlight_data.cont_main);
        lv_obj_set_pos(lvgl_backlight_data.cont_main, lvgl_backlight_data.main_cont_x, -(lv_obj_get_height(parent)));


        lv_obj_set_style_local_bg_opa(lvgl_backlight_data.cont_main, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT,LV_OPA_90);
        lv_obj_set_style_local_radius(lvgl_backlight_data.cont_main, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT,20);

        lv_obj_set_style_local_bg_color(lvgl_backlight_data.cont_main, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_MAKE(0xa5, 0x46, 0xcc));
        lv_obj_set_style_local_bg_grad_color(lvgl_backlight_data.cont_main, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_MAKE(0xa5, 0x46, 0xcc));


        //----创建容器----//



        lvgl_backlight_data.label1 = lv_label_create(lvgl_backlight_data.cont_main, NULL);
        lv_obj_set_pos(lvgl_backlight_data.label1, 0, 0);
        lv_label_set_long_mode(lvgl_backlight_data.label1, LV_LABEL_LONG_EXPAND);
        lv_obj_set_size(lvgl_backlight_data.label1, 240,130);
        lv_label_set_recolor(lvgl_backlight_data.label1, true);
        lv_label_set_align(lvgl_backlight_data.label1, LV_LABEL_ALIGN_CENTER);
        lv_label_set_recolor(lvgl_backlight_data.label1, true);
        lv_label_set_text(lvgl_backlight_data.label1, "00%");
        lv_obj_add_style(lvgl_backlight_data.label1, LV_OBJ_PART_MAIN, &lvgl_style.font48_white_bg);
        lv_obj_align(lvgl_backlight_data.label1, NULL, LV_ALIGN_IN_TOP_MID, 0, 5);


        lvgl_backlight_data.bar = lv_bar_create(lvgl_backlight_data.cont_main, NULL);
        lv_obj_set_size(lvgl_backlight_data.bar, 260, 24);
        lv_obj_set_pos(lvgl_backlight_data.bar, 0, 0);
        //lv_obj_add_style(lvgl_backlight_data.bar, LV_OBJ_PART_MAIN, &lvgl_style.cont_transparent);
        lv_obj_align(lvgl_backlight_data.bar, NULL, LV_ALIGN_CENTER, 0,20);
        lv_bar_set_range(lvgl_backlight_data.bar, 0, 100);
        lv_bar_set_anim_time(lvgl_backlight_data.bar, 300);

        lv_obj_set_style_local_bg_color(lvgl_backlight_data.bar, LV_BAR_PART_BG, LV_STATE_DEFAULT, LV_COLOR_MAKE(0x4c,0x14,0x66));
        lv_obj_set_style_local_bg_grad_color(lvgl_backlight_data.bar, LV_BAR_PART_BG, LV_STATE_DEFAULT, LV_COLOR_MAKE(0x4c, 0x14, 0x66));
        lv_obj_set_style_local_bg_opa(lvgl_backlight_data.bar, LV_BAR_PART_BG, LV_STATE_DEFAULT, LV_OPA_90);

        lv_obj_set_style_local_bg_color(lvgl_backlight_data.bar, LV_BAR_PART_INDIC, LV_STATE_DEFAULT, LV_COLOR_MAKE(0xff, 0xff, 0xff));
        lv_obj_set_style_local_bg_grad_color(lvgl_backlight_data.bar, LV_BAR_PART_INDIC, LV_STATE_DEFAULT, LV_COLOR_MAKE(0xff, 0xff, 0xff));
        lv_obj_set_style_local_bg_opa(lvgl_backlight_data.bar, LV_BAR_PART_INDIC, LV_STATE_DEFAULT, LV_OPA_90);




        lv_bar_set_value(lvgl_backlight_data.bar, system_data.backlight, LV_ANIM_ON);

        lvgl_backlight_set_backlight(system_data.backlight);


        lvgl_backlight_debug("create win");
    }
    else
    {

        lvgl_backlight_debug("show win");
    }

    lvgl_task_create(&lvgl_backlight_data.task,lvgl_backlight_task_cb, 500, LV_TASK_PRIO_HIGH, NULL);

    lv_obj_move_foreground(lvgl_backlight_data.cont_main);
    lvgl_set_obj_show(&lvgl_backlight_data.cont_main);

    lvgl_backlight_anim_in_start(0, 0);


}

static void lvgl_backlight_set_backlight(int dat)
{
    sprintf(lvgl_backlight_data.p_buf,"%02d%%",dat);

    system_set_backlight(dat);

    lv_label_set_text(lvgl_backlight_data.label1, lvgl_backlight_data.p_buf);
    lv_obj_align(lvgl_backlight_data.label1, NULL, LV_ALIGN_IN_TOP_MID, 0, 5);
    lv_bar_set_value(lvgl_backlight_data.bar, dat, LV_ANIM_ON);
}

void lvgl_backlight_hidden(void)
{

}

void lvgl_backlight_close(void)
{
    lvgl_backlight_debug("close");
    lvgl_group_remove_obj(&lvgl_backlight_data.cont_main);

    lvgl_group_focus_save_obj();

    lvgl_task_delete(&lvgl_backlight_data.task);
    lvgl_obj_clean(&lvgl_backlight_data.cont_main);
    lvgl_obj_del(&lvgl_backlight_data.cont_main);


    if(lvgl_backlight_data.close_data.num == 0)//正常退出
    {

        #if USE_WIN == 0
            system_save_config();
        #endif
    }else
    if(lvgl_backlight_data.close_data.num == 1)//关机退出
    {


    }else
    if(lvgl_backlight_data.close_data.num == 2)//倒计时中退出
    {


    }
}


void lvgl_backlight_close_task(int num)
{
    lvgl_backlight_data.close_data.flag = true;
    lvgl_backlight_data.close_data.count = 1;
    lvgl_backlight_data.close_data.num = num;
    lvgl_task_create(&lvgl_backlight_data.close_data.task, lvgl_backlight_close_task_cb, 10, LV_TASK_PRIO_HIGH, NULL);
}



